package web;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;

public class UserController {

    public static void main(String[] args) {
        //通过工厂获取service层对象
        //① 获取一个ioc容器（本质是一个工厂）
//        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring.xml");

//        //② 通过工厂ioc容器获取想要的bean对象
//        UserService userService = (UserService) ioc.getBean("userService");
//
//        System.out.println(userService);
//
//        userService = (UserService) ioc.getBean("userService");
//
//        System.out.println(userService);
//
//        //执行ioc的close，实现优雅停机
//        ioc.close();

        SqlSessionFactory factory = (SqlSessionFactory) ioc.getBean("sqlSessionFactory2");
        SqlSession sqlSession = factory.openSession();
        System.out.println(sqlSession);
        sqlSession.close();
    }
}
